import line from 'helpers/line';

const GET_SCHEMAS_SUCCESS = 'GET_SCHEMAS_SUCCESS';
const GET_SCHEMAS_FAIL = 'GET_SCHEMAS_FAIL';
const WORKING = 'WORKING';

const initialState = {
  data: null,
  error: null
};

export default function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case WORKING:
      return {
        ...state,
        working: true
      };
    case GET_SCHEMAS_SUCCESS:
      return {
        ...state,
        data: action.result.results
      };
    case GET_SCHEMAS_FAIL:
      return {
        ...state,
        error: action.error
      };
    default:
      return state;
  }
}

export function getSchemas() {
  return {
    types: [WORKING, GET_SCHEMAS_SUCCESS, GET_SCHEMAS_FAIL],
    promise: () => line.getSchemas()
  };
}
